﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Nntp
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Threading;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Win32;

    /// <summary>Provides the means to connect to a test server that requires authentication.</summary>
    internal static class TestServerHelper
    {
        private const string RegistryKey = "HKEY_CURRENT_USER\\Software\\Phuse\\TestServer";
        private const string ServerNameValueName = "ServerName";
        private const string AccountNameValueName = "AccountName";
        private const string PasswordValueName = "Password";

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for proper disposal.")]
        internal static Client GetTestServerClient()
        {
            try
            {
                using (var clientGuard = ScopeGuard.Create(new Client(ServerName, CancellationToken.None)))
                {
                    new AuthInfoCommand(AccountName, Password).Execute(clientGuard.Resource);
                    clientGuard.Dismiss();
                    return clientGuard.Resource;
                }
            }
            catch (ArgumentException)
            {
            }
            catch (AuthenticationFailedException)
            {
            }
            catch (IOException)
            {
            }

            Assert.Inconclusive("Test server is not available.");
            return null;
        }

        internal static string ServerName
        {
            get { return GetValue(ServerNameValueName); }
        }

        internal static string AccountName
        {
            get { return GetValue(AccountNameValueName); }
        }

        internal static string Password
        {
            get { return GetValue(PasswordValueName); }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private static string GetValue(string valueName)
        {
#if WINDOWS_PHONE
            try
            {
                return (string)IsolatedStorageSettings.ApplicationSettings[valueName];
            }
            catch (KeyNotFoundException)
            {
                return null;
            }
#else
            return (string)Registry.GetValue(RegistryKey, valueName, null);
#endif
        }

        private static void SetValue(string valueName, string value)
        {
#if WINDOWS_PHONE
            IsolatedStorageSettings.ApplicationSettings[valueName] = value;
            IsolatedStorageSettings.ApplicationSettings.Save();
#else
            Registry.SetValue(RegistryKey, valueName, value);
#endif
        }
    }
}
